<!DOCTYPE html>
<html>
<style type=text/css>
.fileinput-button {
	position: relative;
	display: inline-block;
	overflow: hidden;
}

.fileinput-button input {
	position: absolute;
	left: 0px;
	top: 0px;
	opacity: 0;
	-ms-filter: 'alpha(opacity=0)';
}

.round_icon {
	width: 100px;
	height: 100px;
	display: flex;
	border-radius: 50%;
	align-items: center;
	justify-content: center;
	overflow: hidden;


.round_new_icon {
	width: 100px;
	height: 100px;
	display: flex;
	border-radius: 50%;
	align-items: center;
	justify-content: center;
	overflow: hidden;
}
</style>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="/static/css/index.css" rel="stylesheet">
<title>单位管理</title>
</head>
<body>


	<div class="inner-title">
		<h3 class="fn f14">
			<img src="/static/images/icon-1.png" alt="" /> 单位管理
		</h3>
	</div>

	<div class="inner-cont">
		<div class="bgf2 creat-wrap" id="newcom">
			<button class="creat-btn cf" onclick="add();">
				<img src="/static/images/icon-add.png" alt="" /> 新增单位
			</button>
		</div>

		<div class="mt30">
			<table border="0" class="table">
				<tr>
					<th width="74">序号</th>
					<th width="500">单位名称</th>
					<th width="500">地址</th>
					<th width="500">状态</th>

					<th width="200">操作</th>
				</tr>

				<tbody id="table">
				</tbody>
			</table>
			<div align="center" style="font-size: 18px; color: gray" id="page">

			</div>
		</div>

	</div>


	<!-- 弹窗 -->
	<div class="pop pa bgf5 hide">
		<span class="pa pop-shut"></span>
		<h3 class="cf pop-title" id="h3">新增单位</h3>
		<div class="pop-cont">
			<form action="/web/companyAdd" enctype="multipart/form-data" method="post"
				class="form form-2 f14" id="form">
				<table class="dialog-table">
					<tr id="esn">
						<td id="img"><span class="fileinput-button" id="input">
								<img src="/static/images/company.jpg" id="jpg"
								class="round_icon" /> <input type="file"
								style="width: 100px; height: 100px;" id="imginput" name="cImg"
								accept=".jpg" />
						</span></td>
					</tr>
					<tr>
						<td><span>点击上方图标选择单位LOGO</span></td>

					</tr>
					<tr>
						<td>单位名称</td>
						<td><input type="text" id="cName" name="cName" />
							<input type="hidden"  name="cId" id="cId" />
						</td>
						<td></td>
					</tr>
					<tr>
						<td>单位具体地址</td>
						<td><input type="text" id="cad" name="cAddress" /></td>
						<td></td>
					</tr>
					<tr>
						<td colspan="3">授权设备</td>
					</tr>
					<tr>
						<td colspan="3">
							<table>
								<tbody id="eqtable"></tbody>
							</table> <span id="eqpage"> </span>
						</td>
					</tr>
					<tr>
						<td></td>
						<td><button id="btn_modifyOrCreate">提交</button></td>
						<td></td>
					</tr>
				</table>
				<div align="center" style="font-size: 18px; color: gray" id="page">

				</div>
			</form>
		</div>
	</div>




	<script src="/static/js/jquery.js" type="text/javascript"></script>
	<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

	<script type="text/javascript">
			var count=0;
			$(function(){
				$.ajax({
					url:"/web/getPageCompany/1",
					success:function(response){
					if(response.success){
						$.each(response.data,function(i,com){
							
								var status;
								var html="";
								if(com.cStatus==0){
									status="正常";
									html='src="/static/images/pause.png" title="冻结"';
								}else{
									status="冻结";
									html='src="/static/images/recover.png" title="恢复"';
								}
								count++;
								$("#table").append('<tr ><td >'+count+'</td><td >'+com.cName+'</td><td>'+com.cAddress+'</td><td>'+status+'<td ><img onclick="upd('+com.cId+',this);" src="/static/images/icon-edit.png" title="编辑" style="cursor:pointer;"/><img style="margin-left: 10px" '+html+'  onclick="pause('+com.cId+',this);" style="cursor:pointer;"/><a href="/web/getPageusertoapge/'+com.cId+'"><img  src="/static/images/icon-setting.png" style="margin-left: 10px" title="人员管理" style="cursor:pointer;"/></a></td></tr>');
								
						});
						if(response.pagenum>1){
							for(var i=1;i<=response.pagenum;i++){
								if(i==1){
									$("#page").append('<button type="button" style="margin-left: 30px" onclick="page(this);" value="'+i+'">首页</button>')
								}else if(i==response.pagenum){
									$("#page").append('<button type="button" style="margin-left: 30px" onclick="page(this);" value="'+i+'">尾页</button>')
								}else{
									$("#page").append('<button type="button" style="margin-left: 30px" onclick="page(this);" value="'+i+'">'+i+'</button>')
								}
							}
						}
					}
				},error:function(){
					alert("出现未知错误,请联系站内管理员!");
				}
					
				});
				if([[${session.user.uIdentity}]]!=1){
					$("#newcom").remove();
				}
				// 展示弹窗
				
				// 关闭弹窗
				$('.pop-shut').on('click', function(e) {
					$(this).parent().hide();
				})
				
				 $("#imginput").change(function(e) {
            var imgBox = e.target;
            uploadImg($('#imginput'), imgBox)
        });
		var is;
        function uploadImg(element, tag) {
            var file = tag.files[0];
            var imgSrc;
            if (file.type!="image/jpeg") {
				
                alert("仅支持jpg格式图片！");
				is=false;
                return false;
            }
            var reader = new FileReader();
            reader.readAsDataURL(file);
         	if(is!=false){
			   reader.onload = function() {
                	console.log(this.result);
                	imgSrc = this.result;
                	var imgs = document.createElement("img");
                	$(imgs).attr("src", imgSrc);
                	$(imgs).attr("class", "round_new_icon");
					$(imgs).attr("id", "jpg");
                	$("#jpg").remove();
                	$("#input").prepend(imgs);
            	};
        	}
		}
			});
			function page(event){
				var a = $(event).val();
				if(a==1){
					count=0;
				}else{
					count=(a-1)*20
				}
				$.ajax({
					url:"/web/getPageEquipments/[[${session.user.cId}]]/"+a,
					success:function(response){
						if(response.success){
							$("#table").empty();
							$.each(response.data,function(i,eq){
								if(eq.eStatus!=2){
									var eStatus ;
									if(eq.eStatus==0){
										eStatus="离线";
									}else if(eq.eStatus==1){
										eStatus="在线";
									}
									var finalHeartbeatTime;
									if(eq.finalHeartbeatTime==null||eq.finalHeartbeatTime==""){
										finalHeartbeatTime="--"
									}else{
										finalHeartbeatTime=eq.finalHeartbeatTime
									}
									count++;
									$("#table").append('<tr ><td >'+count+'</td><td >com.cName</td><td>'+com.cAddress+'</td><td>'+status+'<td ><img onclick="upd('+com.cId+',this);" src="/static/images/icon-edit.png" title="编辑" style="cursor:pointer;"/><img style="margin-left: 10px" '+html+'  onclick="pause('+com.cId+',this);" style="cursor:pointer;"/><a href="/web/getPageusertoapge/'+com.cId+'"><img  src="/static/images/icon-setting.png" style="margin-left: 10px" title="人员管理" style="cursor:pointer;"/></a></td></tr>');
									
								}
							});
						}
					},error:function(){
						alert("出现未知错误,请联系站内管理员!");
					}
				});
			}
			function pause(cid,event) {
				$.ajax({
					url:"/web/pauseCompany/"+cid,
					success:function(response){
						if(response.success){
							$(event).parent().prev().html("冻结");
							$(event).attr("src","/static/images/recover.png");
							$(event).attr("title","恢复");
							$(event).attr("onclick","recover("+cid+",this);");
						}
					},error:function(){
						alert("冻结失败");
					}
				});
			}
			function recover(cid,event) {
				$.ajax({
					url:"/web/recoverCompany/"+cid,
					success:function(response){
						if(response.success){
							$(event).parent().prev().html("正常");
							$(event).attr("src","/static/images/pause.png");
							$(event).attr("title","冻结");
							$(event).attr("onclick","pause("+cid+",this);");
						}
					},error:function(){
						alert("恢复失败");
					}
				});
			}
			function upd(cid,event) {
				$("#eqtable").empty();
				$("#eqpage").empty();
				$("#h3").text("修改单位信息");
				$.ajax({
					url:"/web/companytoUpd/"+cid,
					success:function(response){
						if(response.success){
							var com=response.data;
							$("#jpg").attr("src","/img/"+com.cImg);
							$("#cName").val(com.cName);
							$("#cad").val(com.cAddress);
							$("#cId").val(com.cId);
							$("#form").attr("action","/web/companytdoUpd");
							$('.pop').show();
							$.ajax({
								url : "/web/getPageEquipments/[[${session.user.cId}]]/1",
								success : function(response) {
									if (response.success) {
										var count = 0;
										var st=com.eSn;
										var eid=new Array();
										eid=st.split('-');
										$.each(response.data, function(i, eq) {
											if (count % 3 == 0) {
												$("#eqtable").append('<tr>');
											}
											$("#eqtable").append(
													'<td><input type="checkbox" id="'+eq.sN+'" name="eSn" value="'+eq.eId+'"> '
															+ eq.eName + '</input></td>');
											if (count % 3 == 0) {
												$("#eqtable").append('</tr>');
											}
											count++;
											for(var j=0;j<eid.length;j++){
												var e=eid[j];
												if(e==eq.sN){
													$("#"+eq.sN).attr("checked","checked");
												}
											
											}
										});
										if (response.pagenum > 1) {
											for (var i = 1; i <= response.pagenum; i++) {
												$("#eqpage").append(
														'<button type="button" style="margin-left: 30px" onclick="updpage('+com.cId+',this);" value="'
																+ i + '">'+i+'</button>');
											}
										}
									}
								},
								error : function() {
									alert("出现未知错误,请联系站内管理员!");
								}

							});
						}
						
					},error:function(){
						
					}
				});
			}
			function add() {
				$("#eqtable").empty();
				$("#eqpage").empty();
				$("#h3").text("新增单位");
				$.ajax({
					url : "/web/getPageEquipments/[[${session.user.cId}]]/1",
					success : function(response) {
						if (response.success) {
							var count = 0;
							$.each(response.data, function(i, eq) {
								if (count % 3 == 0) {
									$("#eqtable").append('<tr>');
								}
								$("#eqtable").append(
										'<td><input type="checkbox" name="eSn" value="'+eq.eId+'"> '
												+ eq.eName + '</input></td>');
								if (count % 3 == 0) {
									$("#eqtable").append('</tr>');
								}
								count++;
							});
							if (response.pagenum > 1) {
								for (var i = 1; i <= response.pagenum; i++) {
									$("#eqpage").append(
											'<button type="button" style="margin-left: 30px" onclick="eqpage(this);" value="'
													+ i + '">'+i+'</button>');
								}
							}
						}
					},
					error : function() {
						alert("出现未知错误,请联系站内管理员!");
					}

				});
				$('.pop').show();
				
			}
			function eqpage(event) {
				var a = $(event).val();
				if (a == 1) {
					count = 0;
				} else {
					count = (a - 1) * 20
				}
				$.ajax({
					url : "/web/getPageEquipments/[[${session.user.cId}]]/" + a,
					success : function(response) {
						if (response.success) {
							$("#eqtable").empty();
							var count = 0;
							$.each(response.data, function(i, eq) {
								if (count % 3 == 0) {
									$("#eqtable").append('<tr>');
								}
								$("#eqtable").append(
										'<td><input type="checkbox" name="eSn" value="'+eq.eId+'"> '
												+ eq.eName + '</input></td>');
								if (count % 3 == 0) {
									$("#eqtable").append('</tr>');
								}
								count++;
							});
						}
					},
					error : function() {
						alert("出现未知错误,请联系站内管理员!");
					}
				});
			}
			$.fn.validate = function(tips){
				var is=false;
			    if($(this).val() == "" || $.trim($(this).val()).length == 0){
			        alert(tips+"不能为空！");
			    }else{
			    	is=true;
			    }
			    return is;
			}



			$('#form').submit(function() {
					var result=false;
			    	if($("#cName").validate("公司名称")==true&&$("#cad").validate("公司地址")==true){
						result=true;
					}
					if(result==false){
						return false;
					}
			 
			});
			 function updpage(cid,event){
				 var a = $(event).val();
				 $("#eqtable").empty();
					$.ajax({
						url:"/web/companytoUpd/"+cid,
						success:function(response){
							if(response.success){
								var com=response.data;
								$.ajax({
									url : "/web/getPageEquipments/[[${session.user.cId}]]/"+a,
									success : function(response) {
										if (response.success) {
											var count = 0;
											var st=com.eSn;
											var eid=new Array();
											eid=st.split('-');
											$.each(response.data, function(i, eq) {
												if (count % 3 == 0) {
													$("#eqtable").append('<tr>');
												}
												$("#eqtable").append(
														'<td><input type="checkbox" id="'+eq.sN+'" name="eSn" value="'+eq.eId+'"> '
																+ eq.eName + '</input></td>');
												if (count % 3 == 0) {
													$("#eqtable").append('</tr>');
												}
												count++;
												for(var j=0;j<eid.length;j++){
													var e=eid[j];
													if(e==eq.sN){
														$("#"+eq.sN).attr("checked","checked");
													}
												
												}
											});
											
										}
									},
									error : function() {
										alert("出现未知错误,请联系站内管理员!");
									}

								});
							}
							
						},error:function(){
							
						}
					});
			 }
</script>
</body>
</html>